package com.ebelter.nb.ui.fragments;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.ebelter.btcomlib.bases.fragment.BaseFragment;
import com.ebelter.btcomlib.models.bluetooth.products.scale.ScaleManager;
import com.ebelter.btcomlib.models.eventbus.CommonEventBus;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.btcomlib.views.LevelColorView;
import com.ebelter.nb.R;
import com.ebelter.nb.beans.GetBC;
import com.ebelter.nb.model.http.request.NbNetUtils;
import com.ebelter.nb.model.jg.jgbean.JGScaleR;
import com.ebelter.nb.ui.activitys.MainActivity;
import com.ebelter.nb.ui.activitys.TrendActivity;
import com.ebelter.nb.ui.view.CombinView5;
import com.ebelter.nb.utils.NbUtitls;

import org.greenrobot.eventbus.EventBus;

/**
 * 血压计
 */

public class Scale_Fragment extends BaseFragment implements View.OnClickListener {
    public static final String TAG = "Scale_Fragment";
    LevelColorView scale_lcv;


    private String titleStr="体脂秤";

    CombinView5 cb5_s_weight;
    CombinView5 cb5_s_fat;
    CombinView5 cb5_s_muscle;
    CombinView5 cb5_s_skeletal;//骨骼肌
    CombinView5 cb5_s_bmr;
    CombinView5 cb5_s_innerfatlevel;
    CombinView5 cb5_s_water;
    CombinView5 cb5_s_guyanliang;
    CombinView5 cb5_s_protein;
    CombinView5 cb5_s_bmi;
    CombinView5 cb5_s_xl;




    LinearLayout scale_trend_ll;
    LinearLayout scale_result_tip_ll;

    private TextView activityTopTittleTv;
    private TextView scale_result_tip_tv;
    private TextView scale_mesure_time_tv;

    private ImageView scale_ic_iv;//男女图标
    private TextView scale_f_tip1_tv;//提示语
    private LinearLayout scale_f_body_ll;//身体得分
    private TextView scale_bodyscore_tv;//身体得分
    private TextView scale_bodyage_tv;//身体年龄



    public String getTitleStr(){
        return titleStr;
    }


    @Override
    protected int setLayoutResouceId() {
        return R.layout.fragment_scale;
    }
    @Override
    protected void initView() {
        super.initView();
        FV();
    }

    @Override
    protected void setListener() {
        scale_trend_ll.setOnClickListener(this);
    }

    // int level;
    private void FV() {
        scale_lcv = mRootView.findViewById(R.id.scale_lcv);
        scale_trend_ll = mRootView.findViewById(R.id.scale_trend_ll);
        scale_result_tip_ll = mRootView.findViewById(R.id.scale_result_tip_ll);
        scale_lcv.setLevel(-1);

        cb5_s_weight = mRootView.findViewById(R.id.cb5_s_weight);
        cb5_s_fat = mRootView.findViewById(R.id.cb5_s_fat);
        cb5_s_muscle = mRootView.findViewById(R.id.cb5_s_muscle);
        cb5_s_bmr = mRootView.findViewById(R.id.cb5_s_bmr);
        cb5_s_innerfatlevel = mRootView.findViewById(R.id.cb5_s_innerfatlevel);
        cb5_s_water = mRootView.findViewById(R.id.cb5_s_water);
        cb5_s_guyanliang = mRootView.findViewById(R.id.cb5_s_guyanliang);
        cb5_s_protein = mRootView.findViewById(R.id.cb5_s_protein);
        cb5_s_bmi = mRootView.findViewById(R.id.cb5_s_bmi);
        scale_result_tip_tv = mRootView.findViewById(R.id.scale_result_tip_tv);
        cb5_s_xl = mRootView.findViewById(R.id.cb5_s_xl);

        scale_ic_iv = mRootView.findViewById(R.id.scale_ic_iv);
        scale_f_tip1_tv = mRootView.findViewById(R.id.scale_f_tip1_tv);
        scale_f_body_ll = mRootView.findViewById(R.id.scale_f_body_ll);
        scale_bodyscore_tv = mRootView.findViewById(R.id.scale_bodyscore_tv);
        scale_bodyage_tv = mRootView.findViewById(R.id.scale_bodyage_tv);

        scale_mesure_time_tv = mRootView.findViewById(R.id.scale_mesure_time_tv);
        cb5_s_skeletal = mRootView.findViewById(R.id.cb5_s_skeletal);
    }



    private void updateSexIcon(){
        int sex = NbUtitls.getSex();
        ViewUtils.setImageResource(scale_ic_iv,sex==0?R.drawable.ic_man:R.drawable.ic_women);
    }


    @Override
    protected void initData(Bundle arguments) {
        initSuggestStrRes();
        getLastScaleMesure();
    }

    private void getLastScaleMesure() {
        if (!NetUtils.available()){
            return;
        }
        String authKey = NbUtitls.getLoginAuthkey();
        NbNetUtils.getInstance().getBodyCompositionPage(this, authKey, 1, 1,"1",
                new HttpResponse<GetBC>() {
                    @Override
                    public void result(boolean isRequestSuccess, String requestDes, GetBC getBC, String s) {
                        dissLoadingDialog();
                        LogUtils.i(TAG,"-getLastScaleMesure--s = "+s);
                        LogUtils.i(TAG,"--getLastScaleMesure-getBc = " + getBC);

                        if (isRequestSuccess&&getBC.resultData!=null&&getBC.resultData.dataList.size()>0){
                            GetBC.ResultDataBean.DataListBean bean=getBC.resultData.dataList.get(0);


                            JGScaleR jgScaleR = new JGScaleR();

                            JGScaleR.DeviceInfoBean d=new JGScaleR.DeviceInfoBean();
                            d.bmi=bean.bmi;
                            d.weight2=bean.weight;
                            d.fat2=bean.adiposerate;
                            d.muscleV=bean.muscle;
                            d.skeletalMuscleV=bean.skeletalMuscle;
                            d.bmr=bean.bmr;
                            d.visceralFat=bean.visceralfat;
                            d.water=bean.moisture;
                            d.boneV=bean.bone;
                            d.protein=bean.protein;
                            d.hart2= (int) bean.hart;
                            d.bodyAge= (int) bean.physicalAge;
                            d.score= (int) bean.bodyScore;

                            d.time=bean.time;
                            jgScaleR.deviceInfo=d;

                            setScaleData(jgScaleR);


                        }

                    }
                });

    }

    private String tizhongguodi4;
    private String tizhongzhengchang5;
    private String tizhongchaozhong4;
    private String qingdufeipang3;
    private String zhongdufeipang3;
    private String zhongdufeipang1;

    private void initSuggestStrRes() {
        tizhongguodi4=getString(R.string.tizhongguodi4);
        tizhongzhengchang5=getString(R.string.tizhongzhengchang5);
        tizhongchaozhong4=getString(R.string.tizhongchaozhong4);
        qingdufeipang3=getString(R.string.qingdufeipang3);
        zhongdufeipang3=getString(R.string.zhongdufeipang3);
        zhongdufeipang1=getString(R.string.zhongdufeipang1);
    }

    @Override
    public void onClick(View v) {
        int vid = v.getId();
        if (vid == R.id.scale_trend_ll){
            Intent intent = new Intent(getContext(), TrendActivity.class);
            intent.putExtra(TrendActivity.KEY,TrendActivity.Trend_TIZHICHENG);
            intent.putExtra(TrendActivity.USER,TrendActivity.Trend_USER_1);
            startActivity(intent);
        }
    }

    //获取BMI建议
    private String getBMISuggestion(float bmi){
        if (bmi<18.5){
            return tizhongguodi4;
        }else if (bmi>=18.5&&bmi<=22.5){
            return tizhongzhengchang5;
        }else if (bmi>=23.0&&bmi<=24.9){
            return tizhongchaozhong4;
        }else if (bmi>=25.0&&bmi<=29.9){
            return qingdufeipang3;
        }else if (bmi>=30.0&&bmi<=39.9){
            return zhongdufeipang3;
        }else {
            return zhongdufeipang1;
        }
    }




    //显示极光推送过来的体脂数据
    public void setScaleData(JGScaleR jGScaleR) {
        updateSexIcon();//更新男女的图标

        ViewUtils.hideView(scale_f_tip1_tv);
        ViewUtils.displayView(scale_f_body_ll);

        ViewUtils.displayView(scale_result_tip_ll);
        ViewUtils.setTextViewStr(scale_result_tip_tv,getBMISuggestion(jGScaleR.deviceInfo.bmi));
        NbUtitls.setCb5Value(cb5_s_weight,2,numFat(jGScaleR.deviceInfo.weight2));
        NbUtitls.setCb5Value(cb5_s_fat,2,numFat(jGScaleR.deviceInfo.fat2));
        NbUtitls.setCb5Value(cb5_s_muscle,2,numFat(jGScaleR.deviceInfo.muscleV));
        NbUtitls.setCb5Value(cb5_s_skeletal,2,numFat(jGScaleR.deviceInfo.skeletalMuscleV));
        NbUtitls.setCb5Value(cb5_s_bmr,2,numFat2(jGScaleR.deviceInfo.bmr));
        NbUtitls.setCb5Value(cb5_s_innerfatlevel,2,numFat(jGScaleR.deviceInfo.visceralFat));
        NbUtitls.setCb5Value(cb5_s_water,2,numFat(jGScaleR.deviceInfo.water));
        NbUtitls.setCb5Value(cb5_s_guyanliang,2,numFat(jGScaleR.deviceInfo.boneV));
        NbUtitls.setCb5Value(cb5_s_protein,2,numFat(jGScaleR.deviceInfo.protein));
        NbUtitls.setCb5Value(cb5_s_bmi,2,numFat(jGScaleR.deviceInfo.bmi));
        NbUtitls.setCb5Value(cb5_s_xl,2,jGScaleR.deviceInfo.hart2+"");

        ViewUtils.setTextViewStr(scale_bodyscore_tv,numFat2(jGScaleR.deviceInfo.score));
        ViewUtils.setTextViewStr(scale_bodyage_tv,numFat2(jGScaleR.deviceInfo.bodyAge));

        int bmiLevel = ScaleManager.getBMILevel(jGScaleR.deviceInfo.bmi);
        if (scale_lcv!=null){scale_lcv.setLevel(bmiLevel);}

        titleStr = ScaleManager.getBMIDesc(jGScaleR.deviceInfo.bmi);

        CommonEventBus commonEventBus = new CommonEventBus(TAG, MainActivity.TAG,88,null);
        EventBus.getDefault().post(commonEventBus);

        ViewUtils.setTextViewStr(scale_mesure_time_tv,"测量时间:"+jGScaleR.deviceInfo.time);
    }



    private static final String H = "0";
    private String numFat(float value){
        if (value<=0){
            return H;
        }else {
            return NumUtils.numBaoLiuWei1(value)+"";
        }
    }

    private String numFat2(float value){
        if (value<=0){
            return H;
        }else {
            return NumUtils.numBaoLiuWei0(value)+"";
        }
    }
}
